/**
 * 
 */
package org.springside.examples.miniweb.web.report.excel;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.Transient;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import org.springside.examples.miniweb.entity.report.AssignedReport;
import org.springside.examples.miniweb.entity.report.CompressorReport;

/**
 * @author Eric 2012-3-18
 * 
 * @since 6.0
 * 
 */
public class PerformanceReportExcelView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String string = "绩效报表_"+model.get("UserName")+"_"+ DateFormatUtils.format(new Date(),
						"yyyy-MM-dd")+".xls";
		response.setHeader("Content-Disposition", "attachment; filename="+ 
				new String(string.getBytes(), "iso8859-1")); 
		@SuppressWarnings("unchecked")
		List<AssignedReport> reportList = (List<AssignedReport>) model.get("PerformanceReportView");
		reportList = reportList == null ? new ArrayList<AssignedReport>() : reportList; 
		HSSFSheet sheet = workbook.createSheet("绩效报表_"+model.get("UserName"));
		HSSFRow header = sheet.createRow(0);
		String[] headers = new String[]{"ID","客户名称","安排人员","预计完成时间","实际完成时间","是否按时完成"};
		for (int i = 0; i < headers.length; i++) {
			header.createCell(i).setCellValue(headers[i]);
		}

		int rowNum = 1;
		for (AssignedReport report : reportList) {
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(report.getReportId());
			row.createCell(1).setCellValue(report.getCustomerName());
			row.createCell(2).setCellValue(report.getAssignedUser());
			row.createCell(3).setCellValue(report.getScheduleFinish() == null ? "" : DateFormatUtils.format(report.getScheduleFinish(),
					"yyyy-MM-dd"));
			row.createCell(4).setCellValue(report.getUpdateTime() == null ? "" : DateFormatUtils.format(report.getUpdateTime(),
					"yyyy-MM-dd"));
			row.createCell(5).setCellValue(report.getEnd().equalsIgnoreCase("intime")?"已按时完成":"未按时完成");
		}
		HSSFRow row = sheet.createRow(rowNum+1);
		row.createCell(0).setCellValue(getStatus(reportList));
	}

	@Transient
	private String getStatus(List<AssignedReport> reports){
		List<AssignedReport> inTimeReports = new ArrayList<AssignedReport>();
		for (AssignedReport assignedReport : reports) {
			if(assignedReport.getEnd().equalsIgnoreCase("intime")){
				inTimeReports.add(assignedReport);
			}
		}
		
		if(reports.size() == 0){
			return "此次搜索共计"+reports.size() +" 条维修记录，  按时完成 "+ inTimeReports.size() +" 条记录， 按时完成率为   0%";
		}
		return "此次搜索共计"+reports.size() +" 条维修记录，  按时完成 "+ inTimeReports.size() +" 条记录， 按时完成率达 " + getPercent(inTimeReports.size(), reports.size()) + "%";
	}
	
	@Transient
	private String getPercent(int a, int b){
		float num= (float)a/b;  
		DecimalFormat df = new DecimalFormat("0.00");
		return df.format(num*100);
	}
}
